<!DOCTYPE html>
<html class="no-js" lang="zh_CN">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
    <title>${system_admin_title}</title>
    #required_css
</head>
<body>#loading

<div class="app $!layout">
    <!-- 头部菜单 -->#header
    <section class="layout">
        <!-- 菜单 -->#sidebar
        <!-- 内容主体 -->
        <section class="container">
            <section class="main-content">
                <!-- 主体操作栏 -->
                <header class="header navbar">
                    <!-- 左按钮区 -->
                    <div class="btn-group tool-button">
                        <button type="button" class="btn btn-primary navbar-btn"
                                onclick="ns.view.showModal('$!basePath/bi/flow/add')"><i class="ti-plus"></i> 创建新流程
                        </button>
                    </div>
                    <!-- 右按钮区 -->
                    <div class="btn-group pull-right"></div>
                </header>
                <div class="content-wrap">
                    <div class="wrapper">
                        <div class="row">
                            <div class="col-lg-12 col-md-12 table-responsive">
                                <table id="dgFlow"></table>
                                <div id="dgFlowPager"></div>
                            </div>
                        </div>
                    </div>
                    <a class="exit-offscreen"></a>
                </div>
            </section>
        </section>
    </section>
</div>
#required_js
<!-- 页面组件 -->

<!-- 本页面脚本 -->
<script src="$!basePath/framework/js/view/toast.js"></script>
<script type="text/javascript">
    ns.requireJS([
        "/framework/js/form/checkbox.js",
        "/framework/js/view/datagrid.js",
        "/framework/js/form/search.form.js"]);
    function deploy(id) {
        jQuery.post("$!basePath/bi/flow/deploy", {id: id}, function (data) {
            if (data.success == true) {
                Toast.success("部署成功！");
                dgFlow.reload();
            } else {
                Toast.error(data.message);
            }
        });
    }
    function unDeploy(id) {
        jQuery.post("$!basePath/bi/flow/undeploy", {id: id}, function (data) {
            if (data.success == true) {
                Toast.success("取消部署成功！");
                dgFlow.reload();
            } else {
                Toast.error(data.message);
            }
        });
    }

    //删除流程定义
    function doDelete(id) {
        ns.confirm("确认删除吗？", function () {
            jQuery.post("$!basePath/bi/flow/delete", {id: id}, function (data) {
                if (data.success == true) {
                    Toast.success("删除成功！");
                    dgFlow.reload();
                } else {
                    Toast.error(data.message);
                }
            });
        });
    }

    var dgFlow;
    ns.ready(function () {
        dgFlow = $("#dgFlow").jqGrid({
            url: '$!basePath/bi/flow/data',
            colModel: [
                {label: 'ID', name: 'id', key: true, hidden: true, align: "left"},
                {label: '名称', name: 'name', width: 100},
                {label: 'Key', name: 'key', width: 100},
                {label: '版本', name: 'version', width: 50},
                {
                    label: '状态',
                    name: 'persistentState.deploymentId',
                    width: 50,
                    formatter: function (val) {
                        if (val && val.length > 0)
                            return "<span class=\"label label-success\">已部署</span>";
                        else return "<span class=\"label label-default\">未部署</span>";
                    }
                },
                {label: '创建时间', name: 'createTime', width: 100},
                {label: '最后更新时间', name: 'lastUpdateTime', width: 100},
                {
                    label: '操作', width: 50, formatter: function (val, node, data) {
                    var opArr = [];

                    opArr.push({title: "流程设计", url: "$!basePath/bi/flow/modeler?modelId=" + data.id, target: "_blank"});

                    if (data.persistentState.deploymentId) {
                        opArr.push({
                            title: "取消部署", onclick: function () {
                                ns.confirm("将删除所有的流程实例和历史记录，确认删除吗？", function () {
                                    unDeploy(data.persistentState.deploymentId);
                                });
                            }
                        });
                    } else {
                        opArr.push({
                            title: "部署", onclick: function () {
                                ns.confirm("确认部署吗？", function () {
                                    deploy(data.id);
                                });
                            }
                        });
                    }

                    opArr.push({divider: true});
                    opArr.push({
                        title: "删除", onclick: function () {
                            doDelete(data.id);
                        }
                    });
                    return ns.view.datagrid.renderOp(opArr);
                }
                }
            ],
            height: $(".main-content .wrapper").height() - ns.view.datagrid.offsetHeight,
            pager: "#dgFlowPager",
            rownumbers: true,
//            searchForm : "#searchForm",
        });
    });
</script>
#loading_close
</body>

</html>